//
//  KLSecondUserTableViewController.m
//  KISSLOCK
//
//  Created by 陈宾鹏 on 16/8/25.
//  Copyright © 2016年 陈宾鹏. All rights reserved.
//

#import "KLSecondUserTableViewController.h"
#import "KLHttpAPI.h"
#import "KLHelp.h"
#import "HemaConst.h"
#import "JRToast.h"
#import "Masonry.h"
#import "HemaFunction.h"
#import "JRToast.h"
#import "KLSecondChildKeyTableViewController.h"
#import "KLEntrustSecondMasterVC.h"
#import "UIActionSheet+Blocks.h"

@interface KLSecondUserTableViewController ()
@property (nonatomic,strong) NSArray *secondUserList;
@end

@implementation KLSecondUserTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self naveSet];
    
    [self refreshSecondUserList];
}

#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.secondUserList.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"secondUser"];
    if(!cell){
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"secondUser"];
    }else{
        //删除cell的所有子视图
        while ([cell.contentView.subviews lastObject] != nil){
            [(UIView*)[cell.contentView.subviews lastObject] removeFromSuperview];
        }
    }
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    NSDictionary *userInfo = self.secondUserList[indexPath.row];
    cell.textLabel.text = [NSString stringWithFormat:@"授权用户昵称:%@",[userInfo objectForKey:@"username"]];
    cell.detailTextLabel.text = [NSString stringWithFormat:@"手机号:%@",[userInfo objectForKey:@"mobile"]];
    
    return cell;
}

-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    return YES;
}

- (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewRowAction *action0 = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"取消授权" handler:^(UITableViewRowAction *action, NSIndexPath *indexPath) {
        // 收回左滑出现的按钮(退出编辑模式)
        tableView.editing = NO;
        UIActionSheet *sheet = [[UIActionSheet alloc]initWithTitle:nil cancelButtonItem:[RIButtonItem itemWithLabel:@"取消" action:nil] destructiveButtonItem:nil otherButtonItems:[RIButtonItem itemWithLabel:@"同时删除该二级锁主下发的子钥匙" action:^{
            NSDictionary *rowOfListDic = self.secondUserList[indexPath.row];
            [KLHelp showHUDAddedTo:self.view animated:YES];
            [KLHttpAPI cancelOneSeconMasterPowerWithLock_id:[self.currentLockDic objectForKey:@"id"] mobile:[rowOfListDic objectForKey:@"mobile"] type:@"1" completionHandler:^(MKNetworkOperation *operation) {
                [KLHelp hideAllHUDsForView:self.view animated:YES];
                NSLog(@"同时删除该二级锁主下发的子钥匙返回数据:%@",operation.responseString);
                NSDictionary *dic = operation.responseJSON;
                NSString *isSuccess = [dic objectForKey:@"success"];
                if(isSuccess.integerValue == 1){
                    [self refreshSecondUserList];
                }
                [KLHelp showTextHUDforView:self.view text:[dic objectForKey:@"msg"]];
            } errorHandler:^(NSString *error) {
                [KLHelp hideAllHUDsForView:self.view animated:YES];
                [KLHelp showTextHUDforView:self.view text:error];
            }];
        }], [RIButtonItem itemWithLabel:@"保留该二级锁主下发的子钥匙" action:^{
            [KLHelp showHUDAddedTo:self.view animated:YES];
            NSDictionary *rowOfListDic = self.secondUserList[indexPath.row];
            [KLHttpAPI cancelOneSeconMasterPowerWithLock_id:[self.currentLockDic objectForKey:@"id"] mobile:[rowOfListDic objectForKey:@"mobile"] type:@"0" completionHandler:^(MKNetworkOperation *operation) {
                [KLHelp hideAllHUDsForView:self.view animated:YES];
                NSLog(@"保留该二级锁主下发的子钥匙返回数据:%@",operation.responseString);
                NSDictionary *dic = operation.responseJSON;
                NSString *isSuccess = [dic objectForKey:@"success"];
                if(isSuccess.integerValue == 1){
                    [self refreshSecondUserList];
                }
                [KLHelp showTextHUDforView:self.view text:[dic objectForKey:@"msg"]];
            } errorHandler:^(NSString *error) {
                [KLHelp hideAllHUDsForView:self.view animated:YES];
                [KLHelp showTextHUDforView:self.view text:error];
            }];
        }],nil];
        [sheet showInView:self.view];
    }];
    action0.backgroundColor = [UIColor redColor];
    
    return @[action0];
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    KLSecondChildKeyTableViewController *secondChildKeyVC  =[[KLSecondChildKeyTableViewController alloc]init];
    secondChildKeyVC.secondLockInfor = self.secondUserList[indexPath.row];
    secondChildKeyVC.lockInfor = self.currentLockDic;
    [self.navigationController pushViewController:secondChildKeyVC animated:YES];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 55.0f;
}

#pragma mark -界面设置
//初始化导航栏
-(void)naveSet{
    self.title = @"授权列表";
    self.view.backgroundColor = RGB_UI_COLOR(242, 242, 242);
    self.tableView.tableFooterView = [[UIView alloc]init];
    
    UIButton *rightBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 100, 40)];
    [rightBtn setTitle:@"授权二级锁主" forState:UIControlStateNormal];
    rightBtn.titleLabel.font = [UIFont systemFontOfSize:15 weight:0.2];
    [rightBtn addTarget:self action:@selector(addSecondMaster:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:rightBtn];
    
    [KLHelp navSet:self doBackHandeler:^(UIButton *backButton) {
        [backButton addTarget:self action:@selector(doBack:) forControlEvents:UIControlEventTouchUpInside];
    }];
}-(void)doBack:(id)sender{
    [self.navigationController popViewControllerAnimated:YES];
}-(void)addSecondMaster:(UIButton *)sender{
    KLEntrustSecondMasterVC *entrustVC = [[KLEntrustSecondMasterVC alloc]initWithNibName:@"KLEntrustSecondMasterVC" bundle:nil];
    entrustVC.currentLockInfo = [self.currentLockDic copy];
    [self.navigationController pushViewController:entrustVC animated:YES];
}

#pragma amrk - 刷新二级锁主列表
-(void)refreshSecondUserList{
    [KLHelp showHUDAddedTo:self.view animated:YES];
    [KLHttpAPI requestSecondUserListWithLock_ID:[self.currentLockDic objectForKey:@"id"] completionHandler:^(MKNetworkOperation *operation) {
        NSLog(@"获取二级锁主列表operation:%@",operation.responseJSON);
        NSString *isSuccess = [operation.responseJSON objectForKey:@"success"];
        if(isSuccess.integerValue == 1){
            [KLHelp hideAllHUDsForView:self.view animated:YES];
            NSArray *infor = [operation.responseJSON objectForKey:@"infor"];
            if(![infor isKindOfClass:[NSNull class]] && infor.count != 0){
                self.secondUserList = [NSArray arrayWithArray:infor];
            }else{
                [KLHelp setNoDateImageWithView:self.view type:0];
            }
            [self.tableView reloadData];
        }else{
            [KLHelp setNoDateImageWithView:self.view type:1];
        }
    } errorHandler:^(NSString *error) {
    }];
}
@end
